import config from '@/lib/config';
import { useMutation } from '@tanstack/react-query';
import Cookies from 'js-cookie';

interface IUpdateProjectConnectorsDto {
    column: string;
    status: boolean;
}

const useUpdateProjectConnectors = () => {    
    const update = async (data: IUpdateProjectConnectorsDto) => {
        const response = await fetch(`${config.API_URL}/project_connectors`, {
            method: 'POST',
            body: JSON.stringify(data),
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${Cookies.get('access_token')}`,
            },
        });

        if (!response.ok) {
            const errorData = await response.json();
            throw new Error(errorData.message || "Unknown error occurred");
        }
        
        return response.json();
        
    };

    const updateProjectConnectorPromise = (data: IUpdateProjectConnectorsDto) => {
        return new Promise(async (resolve, reject) => {
            try {
                const result = await update(data);
                resolve(result);
                
            } catch (error) {
                reject(error);
            }
        });
    };

    return {
        mutate: useMutation({
            mutationFn: update,
        }),
        updateProjectConnectorPromise,
    };
};

export default useUpdateProjectConnectors;
